// Copyright tang.  All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package middle.ground.mapper;

import middle.ground.entity.AssignmentJobEntity;
import middle.ground.model.ops.OpsTaskJobTrend;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface AssignmentJobMapper extends Mapper<AssignmentJobEntity> {

	@Select("SELECT \n"
			+ "\t DATE_FORMAT(create_time,'%Y-%m-%d') as of_date , \n"
			+ "\t count(*) as count_of_job,\n"
			+ "\t count(DISTINCT assignment_id) as count_of_task \n"
			+ " FROM \n"
			+ "( \n"
			+ "\t SELECT * FROM DBSWITCH_ASSIGNMENT_JOB\n"
			+ "\t WHERE DATE_SUB( CURDATE(), INTERVAL ${days} DAY ) <= date(create_time)\n"
			+ ") t \n"
			+ " GROUP BY of_date ")
	@Results({
			@Result(column = "of_date", property = "dateOfDay"),
			@Result(column = "count_of_job", property = "countOfJob"),
			@Result(column = "count_of_task", property = "countOfTask")
	})
	List<OpsTaskJobTrend> queryTaskJobTrend(@Param("days") Integer days);

	@Select("SELECT job_key FROM dbswitch_assignment_job WHERE assignment_id=#{assignmentId} GROUP BY job_key")
	@Results({
			@Result(column = "job_key", property = "jobKey"),
	})
	List<AssignmentJobEntity> getDistinctByAssignmentId(@Param("assignmentId") Long assignmentId);

	@Select("SELECT * FROM dbswitch_assignment_job WHERE job_key=#{jobName} ORDER BY id DESC limit 1")
	AssignmentJobEntity getByJobName(@Param("jobName") String jobName);
}
